package com.pureut.quality.controller;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.pureut.common.core.constant.Constants;
import com.pureut.common.core.web.controller.BaseController;

import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.quality.domain.ExternalSuggestedReport;
import com.pureut.quality.domain.dto.ExternalSuggestedReportDto;
import com.pureut.quality.domain.vo.ExternalAuditPlanVo;
import com.pureut.quality.domain.vo.ExternalSuggestedReportVo;
import com.pureut.quality.export.ExternalAuditPlanExport;
import com.pureut.quality.export.ExternalSuggestedReportExport;
import com.pureut.quality.service.IExternalSuggestedReportService;
import com.pureut.quality.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 外审建议项报告Controller
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/externalSuggestedReport")
public class ExternalSuggestedReportController extends BaseController
{
    @Resource
    private IExternalSuggestedReportService externalSuggestedReportService;

    /**
     * 查询外审建议项报告列表
     */
    @RequiresPermissions("@ss.hasPermi('quality:externalSuggestedReport:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExternalSuggestedReportVo externalSuggestedReport)
    {
        startPage();
        List<ExternalSuggestedReportDto> list = externalSuggestedReportService.selectExternalSuggestedReportList(externalSuggestedReport);
        return getDataTable(list);
    }


    /**
     * 获取外审建议项报告详细信息
     */
//    @RequiresPermissions("@ss.hasPermi('system:report:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(externalSuggestedReportService.selectExternalSuggestedReportById(id));
    }

    /**
     * 新增外审建议项报告
     */
//    @RequiresPermissions("@ss.hasPermi('system:report:add')")
    @Log(title = "外审建议项报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExternalSuggestedReportVo externalSuggestedReport)
    {
        return toAjax(externalSuggestedReportService.insertExternalSuggestedReport(externalSuggestedReport));
    }

    /**
     * 修改外审建议项报告
     */
//    @RequiresPermissions("@ss.hasPermi('system:report:edit')")
    @Log(title = "外审建议项报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExternalSuggestedReportVo externalSuggestedReport)
    {
        return toAjax(externalSuggestedReportService.updateExternalSuggestedReport(externalSuggestedReport));
    }

    /**
     * 删除外审建议项报告
     */
//    @RequiresPermissions("@ss.hasPermi('system:report:remove')")
    @Log(title = "外审建议项报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String ids)
    {
        return toAjax(externalSuggestedReportService.deleteExternalSuggestedReportByIds(ids));
    }


    /**
     * 生成外审建议项报告导入模板
     */
    @PostMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        String name = "外审建议项报告";
        String fileName = Constants.STATIC_URL + "导入" + name + ".xlsx";
        File file = new File(fileName);
        ByteArrayOutputStream baos = null;
        ServletOutputStream out = null;
        try {

            InputStream inStream=new FileInputStream(file);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition","attachment;filename=" + URLEncoder.encode("导入外审建议项报告.xlsx","UTF-8"));
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.flush();
            baos.close();
            out.flush();
            out.close();
        }
    }

    /**
     * 导出
     */
    @PostMapping("/selectSuggestedReportExport")
    public AjaxResult selectSuggestedReportExport(HttpServletResponse response, ExternalSuggestedReportVo externalSuggestedReportVo) throws IOException {
        List<ExternalSuggestedReportExport> externalSuggestedReportExports = externalSuggestedReportService.selectSuggestedReportExport(externalSuggestedReportVo);
        ExcelUtil<ExternalSuggestedReportExport> excelUtil = new ExcelUtil<ExternalSuggestedReportExport>(ExternalSuggestedReportExport.class);
        excelUtil.excelWriter(response, externalSuggestedReportExports, "外审建议项报告导出.xlsx");
        return AjaxResult.success();
    }

    /**
     * 外审建议项报告导入
     */
    @PostMapping("/importSuggestedReport")
    @Log(title = "外审建议项报告导入", businessType = BusinessType.INSERT)
    public AjaxResult importSuggestedReport(MultipartFile file) throws IOException {
        ExcelUtil<ExternalSuggestedReportExport> excelUtil = new ExcelUtil<ExternalSuggestedReportExport>(ExternalSuggestedReportExport.class);
        List<ExternalSuggestedReportExport> coCustomer = excelUtil.importExcel(file.getInputStream());
        return AjaxResult.success(externalSuggestedReportService.importSuggestedReport(coCustomer));
    }
}
